package cn.chainfind.common.core.service;

import org.springframework.beans.factory.annotation.Value;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName JwtToken
 * @Author zcb
 * @Date 2021年6月17日09:20:28
 **/
//@Component
public class JwtToken {

    private static String jwtKey;
    private static Integer expiredTimeIn;
    private static Integer defaultScope = 8;

    @Value("${token.jwt-key}")
    public void setJwtKey(String jwtKey) {
        JwtToken.jwtKey = jwtKey;
    }

    @Value("${token.token-expired-in}")
    public void setExpiredTimeIn(Integer expiredTimeIn) {
        JwtToken.expiredTimeIn = expiredTimeIn;
    }

//    public static Optional<Map<String, Claim>> getClaims(String token) {
//        DecodedJWT decodedJWT;
//        Algorithm algorithm = Algorithm.HMAC256(JwtToken.jwtKey);
//        JWTVerifier jwtVerifier = JWT.require(algorithm).build();
//        try {
//            decodedJWT = jwtVerifier.verify(token);
//        } catch (JWTVerificationException e) {
//            return Optional.empty();
//        }
//        return Optional.of(decodedJWT.getClaims());
//    }

//    public static String getExpireTime(String token){
//        return DateUtil.formatDateTime(JWT.decode(token).getExpiresAt());
//    }



    public static String makeToken(Long uid) {
        return JwtToken.getToken(uid, JwtToken.defaultScope);
    }

    private static String getToken(Long uid, Integer scope) {
//        Algorithm algorithm = Algorithm.HMAC256(JwtToken.jwtKey);
//        Map<String,Date> map = JwtToken.calculateExpiredIssues();
//
//        return JWT.create()
//                .withClaim("uid", uid)
//                //.withClaim("uName", uName)
//                .withExpiresAt(map.get("expiredTime"))
//                .withIssuedAt(map.get("now"))
//                .sign(algorithm);
        return "";
    }

    private static Map<String, Date> calculateExpiredIssues() {
        Map<String, Date> map = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        Date now = calendar.getTime();
        calendar.add(Calendar.SECOND, JwtToken.expiredTimeIn);
        map.put("now", now);
        map.put("expiredTime", calendar.getTime());
        return map;
    }

	/***
	 * 根据token获得uid
	 * @return uid
	 */
	public static Long getUidByToken(){
//		String bearerToken =  ServletUtils.getRequest().getHeader("Authorization");
//		if (StringUtils.isEmpty(bearerToken)) {
//            throw new AccountExpiredException(ApiCode.AUTHENTICATION_EXCEPTION.getMessage());
//		}
//
//		if (!bearerToken.startsWith("Bearer")) {
//            throw new AccountExpiredException(ApiCode.AUTHENTICATION_EXCEPTION.getMessage());
//		}
//		String[] tokens = bearerToken.split(" ");
//		if (!(tokens.length == 2)) {
//            throw new AccountExpiredException(ApiCode.AUTHENTICATION_EXCEPTION.getMessage());
//		}
//		String token = tokens[1];
//
//		Optional<Map<String, Claim>> optionalMap = JwtToken.getClaims(token);
//		Map<String, Claim> map = optionalMap
//			.orElseThrow(() -> new AccountExpiredException(ApiCode.UNAUTHORIZED.getMessage()));
//
//		Long uid = map.get("uid").asLong();
		Long uid = Long.valueOf(1);
		return  uid;
	}

//	public static String currentToken(){
//        String bearerToken =  ServletUtils.getRequest().getHeader("Authorization");
//        if (StringUtils.isEmpty(bearerToken)) {
//            throw new AccountExpiredException(ApiCode.AUTHENTICATION_EXCEPTION.getMessage());
//        }
//
//        if (!bearerToken.startsWith("Bearer")) {
//            throw new AccountExpiredException(ApiCode.AUTHENTICATION_EXCEPTION.getMessage());
//        }
//        String[] tokens = bearerToken.split(" ");
//        if (!(tokens.length == 2)) {
//            throw new AccountExpiredException(ApiCode.AUTHENTICATION_EXCEPTION.getMessage());
//        }
//        String token = tokens[1];
//        return token;
//    }


}
